package com.findbus.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

public class ShowAdaper extends BaseAdapter {
	private List<Map<String, Object>> mData;
	private LayoutInflater inflater;
	private ImageShow imageShow;
	private String itemType;
	private int[] mResource;
	private String[] mFrom;
	private int[] mTo;

	public ShowAdaper(Context context, List<Map<String, Object>> mData, int[] mResource, String[] mFrom, int[] mTo,
			String itemType) {
		super();
		this.mTo = mTo;
		this.mFrom = mFrom;
		this.mData = mData;
		this.mResource = mResource;
		this.itemType = itemType;
		this.inflater = LayoutInflater.from(context);
	}

	public ShowAdaper(Context context, List<Map<String, Object>> mData, int mResource, String[] mFrom, int[] mTo) {
		this(context, mData, new int[] { mResource }, mFrom, mTo, null);
	}

	public void upadate(List<Map<String, Object>> otherData, boolean isClear) {
		if (isClear)
			this.mData.clear();
		this.mData.addAll(otherData);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return super.getItemViewType(position);
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return mResource.length;
	}

	public void setmData(List<Map<String, Object>> mData) {
		// TODO Auto-generated method stub
		this.mData = mData;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return createViewFromResource(position, convertView, parent, mResource);
	}

	private View createViewFromResource(int position, View convertView, ViewGroup parent, int[] mResource) {
		// TODO Auto-generated method stub
		View v;
		int now_type;
		try {
			now_type = (Integer) mData.get(position).get(itemType);
		} catch (Exception e) {
			now_type = 0;
		}
		if (convertView == null) {
			v = createConvertView(position, parent, mResource, now_type);
		} else {
			v = convertView;
		}
		bindView(position, v);

		return v;
	}

	private View createConvertView(int position, ViewGroup parent, int[] mResource, int now_type) {
		// TODO Auto-generated method stub
		try {
			if (now_type >= mResource.length)
				throw new Exception("没有找到item_type请检查你的输入值");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return createConvertView(position, parent, mResource[now_type], now_type);
	}

	private View createConvertView(int position, ViewGroup parent, int mResource, int now_type) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(mResource, parent, false);
		if (v != null) {
			ViewHolder mHolder = new ViewHolder();
			for (int i : mTo) {
				View fId = v.findViewById(i);
				if (fId != null)
					mHolder.setItem(fId.getClass().cast(fId));
			}
			v.setTag(mHolder);
		}
		return v;
	}

	// 填充组件内容
	private void bindView(int position, View view) {
		// TODO Auto-generated method stub
		final Map<String, ?> dataSet = mData.get(position);
		if (dataSet == null)
			return;
		List<View> mHolder = ((ViewHolder) view.getTag()).getmHolder();
		final String[] from = mFrom;
		final int[] to = mTo;
		final int count = to.length;
		for (int i = 0; i < count; i++) {
			final View v = view.findViewById(to[i]);
			if (v != null) {
				View vi = mHolder.get(i);
				final Object data = dataSet.get(from[i]);
				String text = data == null ? "" : data.toString();
				if (text == null) {
					text = "";
				}
				int visible = 0;
				if (data instanceof Integer)
					visible = (Integer) data;
				if (visible == View.GONE || visible == View.VISIBLE || visible == View.INVISIBLE)
					vi.setVisibility(visible);
				else {
					if (vi instanceof Checkable) {
						if (data instanceof Boolean) {
							((Checkable) v).setChecked((Boolean) data);
						} else if (v instanceof TextView) {
							setViewText((TextView) v, text);
						} else {
							throw new IllegalStateException(
									v.getClass().getName() + " should be bound to a Boolean, not a "
											+ (data == null ? "<unknown type>" : data.getClass()));
						}
					} else if (vi instanceof TextView) {
						setViewText((TextView) v, text);
					} else if (vi instanceof ImageView) {
						if (data instanceof Integer) {
							setViewImage((ImageView) v, (Integer) data);
						} else if (data instanceof String) {
							setViewImage((ImageView) v, text);
						} else if (data instanceof Bitmap) {
							setViewImage((ImageView) v, (Bitmap) data);
						} else {
							throw new IllegalStateException(v.getClass().getName() + " is not a "
									+ " view that can be bounds by this SimpleAdapter");
						}
					}
				}
			}
		}
	}

	// 设置ViewImage图片
	public void setViewImage(ImageView v, int value) {
		v.setImageResource(value);
	}

	public void setViewImage(ImageView v, Bitmap value) {
		v.setImageBitmap(value);
	}

	public void setViewImage(ImageView v, String value) {
		if (value.startsWith("http://") || imageShow != null)
			imageShow.imageShow(v, value);
		else
			try {
				v.setImageResource(Integer.parseInt(value));
			} catch (NumberFormatException nfe) {
				v.setImageURI(Uri.parse(value));
			}
	}

	// 设置TextView文本
	public void setViewText(TextView v, String text) {
		v.setText(text);
	}

	// 设置

	class ViewHolder {
		private List<View> mHolder = new ArrayList<View>();

		private void setItem(View value) {
			// TODO Auto-generated method stub
			mHolder.add(value);
		}

		private List<View> getmHolder() {
			return mHolder;
		}
	}

	public void setImageShow(ImageShow imageShow) {
		this.imageShow = imageShow;
	}

	public interface ImageShow {
		void imageShow(ImageView v, String url);
	}

}
